﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.DataAccess;
using YiYiTong.Rocket.EFDBFirst;
using System.IO;
using YiYiTong.Rocket.Business.Biz;

namespace YiYiTong.Rocket.Business
{
    public class ReleaseBiz
    {
        #region 单例模式
        private ReleaseBiz()
        {
        }

        private static ReleaseBiz _instance;
        private static readonly object _syncObject = new object();

        public static ReleaseBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new ReleaseBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式


        #region 更新列表
        /// <summary>
        /// 更新列表
        /// </summary>
        /// <returns></returns>
        public void SetNewReleaseNo(ReleaseListType listType)
        {
            if (listType == ReleaseListType.Default)
                return;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var release = dbcontext.ADZ_ListRelease.FirstOrDefault(t => t.list_type == (int)listType);
            if(release != null)
            {
                release.release_no = Utils.GetTimeStamp().ToString();
                dbcontext.SaveChangesAsync();
            }
            else
            {
                LogHelper.Error($"当前配置ReleaseListType {listType}无效");
            }
        }
        #endregion


        /// <summary>
        /// 通过name可以找type
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetLabelNameTypeDic()
        {
            Dictionary<string, string> LabelDic = new Dictionary<string, string>();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var lablllist = dbcontext.ADZ_LabelConfig.OrderBy(t => t.sort_id).ToList();
            lablllist.ForEach(t =>
            {
                LabelDic.Add(t.label_name, t.label_type.ToString());
            });
            return LabelDic;
        }

        /// <summary>
        /// 通过type找name
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetLabelTypeNameDic()
        {
            Dictionary<string, string> LabelDic = new Dictionary<string, string>();
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var lablllist = dbcontext.ADZ_LabelConfig.OrderBy(t => t.sort_id).ToList();
            lablllist.ForEach(t =>
            {
                LabelDic.Add(t.label_type.ToString(), t.label_name);
            });
            return LabelDic;
        }

    }
}
